//
//  JLTextureLayout.swift
//  Chain
//
//  Created by Jiang Chencheng on 2024/8/20.
//

import AsyncDisplayKit

/**
 *  快速创建分割线
 */
@inlinable
public func JLLineNodeMake(_ preferredSize: CGSize, color: UIColor = UIColor.black.withAlphaComponent(0.06)) -> ASDisplayNode {
    let lineNode = ASDisplayNode()
    lineNode.backgroundColor = color
    lineNode.style.preferredSize = preferredSize
    return lineNode
}

/**
 *  快速创建文本控件
 */
@inlinable
public func JLTextNodeMake(string: String?, attrubites: [NSAttributedString.Key: Any]? = nil) -> ASTextNode {
    let textNode = ASTextNode()
    if let string = string {
        textNode.attributedText = NSAttributedString(string: string, attributes: attrubites)
    }
    return textNode
}

/**
 *  快速创建文本控件
 */
@inlinable
public func JLTextNodeMake(string: String?, font: UIFont = .systemFont(ofSize: 16), color: UIColor = .black) -> ASTextNode {
    let textNode = ASTextNode()
    if let string = string {
        textNode.attributedText = NSAttributedString(string: string, attributes: [
            .font: font,
            .foregroundColor: color,
        ])
    }
    return textNode
}


/**
 *  快速创建图片控件
 */
@inlinable
public func JLImageNodeMake(image: UIImage) -> ASImageNode {
    let imageNode = ASImageNode()
    imageNode.image = image
    imageNode.style.preferredSize = image.size
    return imageNode
}


/**
 *  快速创建按钮控件
 */
@inlinable
public func JLButtonNodeMake(title: NSAttributedString?, image: UIImage?) -> ASButtonNode {
    let buttonNode = ASButtonNode()
    buttonNode.setAttributedTitle(title, for: .normal)
    buttonNode.setImage(image, for: .normal)
    return buttonNode
}


/**
 *  比例布局
 */
@inlinable
public func JLRatioSpec(child: ASLayoutElement, ratio: CGFloat) -> ASRatioLayoutSpec {
    let ratioLayout = ASRatioLayoutSpec(ratio: ratio, child: child)
    ratioLayout.style.flexShrink = 1.0
    ratioLayout.style.flexGrow = 1.0
    return ratioLayout
}


/**
 *  比例布局
 */
@inlinable
public func JLRatioSpec(child: ASImageNode) -> ASRatioLayoutSpec {
    let ratio = (child.image?.size.height ?? 0) / (child.image?.size.width ?? 0)
    return JLRatioSpec(child: child, ratio: ratio)
}

/**
 *  中心布局
 */
@inlinable
public func JLCenterSpec(child: ASLayoutElement, centeringOptions:ASCenterLayoutSpecCenteringOptions = .XY) -> ASCenterLayoutSpec {
    ASCenterLayoutSpec(centeringOptions: centeringOptions, child: child)
}

/**
 *  覆盖布局
 */
@inlinable
public func JLOverlaySpec(child: ASLayoutElement, overlay:ASLayoutElement) -> ASOverlayLayoutSpec {
    ASOverlayLayoutSpec(child: child, overlay: overlay)
}

/**
 *  背景布局
 */
@inlinable
public func JLBackgroundSpec(child: ASLayoutElement, background:ASLayoutElement) -> ASBackgroundLayoutSpec {
    ASBackgroundLayoutSpec(child: child, background: background)
}

/**
 *  背景布局
 */
@inlinable
public func JLInsetSpec(insets: UIEdgeInsets = .zero ,child: ASLayoutElement) -> ASInsetLayoutSpec {
    ASInsetLayoutSpec(insets: insets, child: child)
}

/**
 *  横向布局
 */
@inlinable
public func JLHStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, justifyContent: ASStackLayoutJustifyContent = .start, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .horizontal, spacing: spacing, justifyContent: .start, alignItems: .center, children: children)
}

/**
 *  横向间距布局
 */
@inlinable
public func JLHSpaceBetweenStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .horizontal, spacing: spacing, justifyContent: .spaceBetween, alignItems: .center, children: children)
}

/**
 *  横向等分布局
 */
@inlinable
public func JLHSpaceAroundStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .horizontal, spacing: spacing, justifyContent: .spaceAround, alignItems: .center, children: children)
}

/**
 *  横向居中布局
 */
@inlinable
public func JLHCenterStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .horizontal, spacing: spacing, justifyContent: .center, alignItems: .center, children: children)
}

/**
 *  横向尾部布局
 */
@inlinable
public func JLHEndStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .horizontal, spacing: spacing, justifyContent: .end, alignItems: .center, children: children)
}


/**
 *  纵向布局
 */
@inlinable
public func JLVStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, justifyContent: ASStackLayoutJustifyContent = .start, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .vertical, spacing: spacing, justifyContent: .start, alignItems: .center, children: children)
}

/**
 *  纵向间距布局
 */
@inlinable
public func JLVSpaceBetweenStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .vertical, spacing: spacing, justifyContent: .spaceBetween, alignItems: .center, children: children)
}

/**
 *  横向等分布局
 */
@inlinable
public func JLVSpaceAroundStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .vertical, spacing: spacing, justifyContent: .spaceAround, alignItems: .center, children: children)
}

/**
 *  纵向居中布局
 */
@inlinable
public func JLVCenterStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .vertical, spacing: spacing, justifyContent: .center, alignItems: .center, children: children)
}

/**
 *  纵向尾部布局
 */
@inlinable
public func JLVEndStackSpec(children: [ASLayoutElement], spacing: CGFloat = 0.0, alignItems: ASStackLayoutAlignItems = .center) -> ASStackLayoutSpec {
    ASStackLayoutSpec(direction: .vertical, spacing: spacing, justifyContent: .end, alignItems: .center, children: children)
}


/**
 *  相对布局
 */
@inlinable
public func JLRelativeSpec(horizontalPosition: ASRelativeLayoutSpecPosition = .center, verticalPosition:ASRelativeLayoutSpecPosition = .center, sizingOption: ASRelativeLayoutSpecSizingOption = .minimumSize, child: ASLayoutElement) -> ASRelativeLayoutSpec {
    ASRelativeLayoutSpec(horizontalPosition: horizontalPosition, verticalPosition: verticalPosition, sizingOption: sizingOption, child: child)
}

/**
 *  纵向底部相对布局
 */
@inlinable
public func JLVEndelativeSpec(horizontalPosition: ASRelativeLayoutSpecPosition = .center, sizingOption: ASRelativeLayoutSpecSizingOption = .minimumSize, child: ASLayoutElement) -> ASRelativeLayoutSpec {
    ASRelativeLayoutSpec(horizontalPosition: horizontalPosition, verticalPosition: .end, sizingOption: sizingOption, child: child)
}

/**
 * 横向底部相对布局
 */
@inlinable
public func JLHEndRelativeSpec(verticalPosition:ASRelativeLayoutSpecPosition = .center, sizingOption: ASRelativeLayoutSpecSizingOption = .minimumSize, child: ASLayoutElement) -> ASRelativeLayoutSpec {
    ASRelativeLayoutSpec(horizontalPosition: .end, verticalPosition: verticalPosition, sizingOption: sizingOption, child: child)
}
